/*
 * @(#)InterLevelConsistencyCheckerImpl.java   10/01/11
 * 
 * Copyright (C) 2009 Ecole des Mines d'Ales
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Contact: yulinz88@gmail.com
 */



/**
 *
 */
package fr.ema.arch3d.analyser.consistencyChecker.consistencyCheckerImpl;

import fr.ema.arch3d.analyser.consistencyChecker.consistencyCheckerItf.InterLevelConsistencyChecker;
import fr.ema.arch3d.jmodel.assembly.Assembly;
import fr.ema.arch3d.jmodel.configuration.Configuration;
import fr.ema.arch3d.jmodel.specification.Specification;
import fr.ema.arch3d.xoadl.configuration.ComponentClassADL;
import fr.ema.arch3d.xoadl.configuration.ConfigurationADL;
import fr.ema.arch3d.xoadl.configuration.ConnectorClassADL;
import fr.ema.arch3d.xoadl.specification.ComponentRoleADL;
import fr.ema.arch3d.xoadl.specification.ConnectionADL;

import java.util.ArrayList;
import java.util.List;

//~--- classes ----------------------------------------------------------------

/**
 * class description
 *
 * @version    version 11 janv. 2010
 * @author     Huaxi (Yulin) ZHANG
 */
public class InterLevelConsistencyCheckerImpl implements InterLevelConsistencyChecker {

    /**
     * Method description
     *
     * @param config
     * @param ass
     */
    public void caConsistencyCheck(Configuration config, Assembly ass) {}

    /**
     * Method description
     *
     * @param cmpRole
     * @param cmpClass
     */
    public void cmpRoleClassCheck(ComponentRoleADL cmpRole, ComponentClassADL cmpClass) {
        Boolean implement = true;
        List    itfRole   = cmpRole.getInterface();
        List    itfClass  = cmpClass.getComponentType().getInterfaceADL();

        for (int i = 0; i < itfRole.size(); i++) {
            if (itfClass.contains(itfRole.get(i))) {
                System.err.println("The component class " + cmpClass.getName()
                                   + " does not implement the component role " + cmpRole.getName());
            }
        }
    }

    /**
     * Method description
     *
     * @param ass
     * @param cmpRole
     */
    public void componentInstantiationCheck(Assembly ass, ComponentRoleADL cmpRole) {

        // TODO Auto-generated method stub
    }

    /**
     * Method description
     *
     * @param connection
     * @param ctrClass
     */
    public void connectionConnectorCheck(ConnectionADL connection, ConnectorClassADL ctrClass) {

        // TODO Auto-generated method stub
    }

    /**
     * Method description
     *
     * @param spec
     * @param config
     */
    public void scConsistencyCheck(Specification spec, Configuration config) {
        List<ComponentClassADL> cmpClasses = new ArrayList();

        if (config.getCompositeComponentClass() != null) {
            cmpClasses.addAll(config.getPrimitiveComponentClass());
        }

        if (config.getPrimitiveComponentClass() != null) {
            cmpClasses.addAll(config.getCompositeComponentClass());
        }

        List<ConfigurationADL.ComponentImplementation> configImpl = config.getComponentImplementation();

        for (int i = 0; i < configImpl.size(); i++) {
            String            cmpClassName = configImpl.get(i).getComponentClassName();
            String            cmpRoleName  = configImpl.get(i).getImplComponentRoleName();
            ComponentClassADL cmpClass     = null;
            ComponentRoleADL  cmpRole      = null;

            for (int j = 0; j < cmpClasses.size(); j++) {
                if (cmpClasses.get(j).getName().equals(cmpClassName)) {
                    cmpClass = cmpClasses.get(j);
                }
            }

            for (int k = 0; k < spec.getComponentRole().size(); k++) {
                if (spec.getComponentRole().get(k).getName().equals(cmpRoleName)) {
                    cmpRole = spec.getComponentRole().get(k);
                }
            }

            cmpRoleClassCheck(cmpRole, cmpClass);
        }
    }
}
